package com.zyd.controller.base;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.service.base.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/base")
public class BaseController {

    @Autowired
    private BaseService service;
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);


    @PostMapping(value = "/getbase", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody getBase(@RequestBody Map<String, Object> map) {
        try {
            String id=(String)map.get("id");
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "请求成功");
            Map<String, Object> data = service.getBase(id);
            resBody.setData(data);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }
    @PostMapping(value = "/getbaselist", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody getBaselist(@RequestBody MultiQueryPageVO request) {
        try {
            PageBean<Map<String, Object>> base = service.getBaselist(request);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "请求成功");
            resBody.setData(base);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }
    @PostMapping(value = "/getbasepage", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody getBasePage(@RequestBody MultiQueryPageVO request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "请求成功");
            PageBean<Map<String, Object>> ret = service.getBasePage(request);
            resBody.setData(ret);
            /**
             * 保存查询的条件 导出使用
             */
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }
    @PostMapping(value = "/add", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addBase(@RequestBody Map<String, Object> map) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "新增成功");
            service.add(map);

            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "新增失败");
        }
    }
    @PostMapping(value = "/update", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateBase(@RequestBody Map<String, Object> map) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "修改成功");
            service.update(map);

            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改失败");
        }
    }
    @PostMapping(value = "/delete", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")

    public ResBody deleteBase(@RequestBody Map<String, Object> map) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "删除成功");
            service.delete(map);

            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除失败");
        }
    }

}
